// components/select-park/select-park.js
import { auditedPark } from "../../api/index";
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    navigationBarTitleBlack: {
      type: Boolean,
      value: false,
    },
    parkName: {
      type: String,
      value: wx.getStorageSync("parkName"),
    },
    managerId:{
      type:String,
      value:wx.getStorageSync('managerId')
    },
    url:{
      type:String,
      value:'/pages/index/index'
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    statusBarHeight: 0,
    barHeight: wx.getMenuButtonBoundingClientRect().bottom,
    parkColumns:[],
  },

  async ready() {
    // await this.getAuditedPark();
    let systemInfo = wx.getSystemInfoSync();
    let menuButtonBoundingClientRect = wx.getMenuButtonBoundingClientRect();
    this.setData({
      statusBarHeight: systemInfo.statusBarHeight,
      navigationBarH:
        menuButtonBoundingClientRect.height +
        (menuButtonBoundingClientRect.top - systemInfo.statusBarHeight) * 2,
    });
  },

  /**
   * 组件的方法列表
   */
  methods: {
    navigateBack() {
      // 暂时改为这个方法，返回跳转首页
      wx.reLaunch({
        url: this.data.url
      });
      // wx.navigateBack({
      //   delta: 1,
      //   fail(err) {
      //     wx.reLaunch({
      //       url: "/pages/index/index",
      //     });
      //   },
      // });
    },
    //   查询已审核的车场
    async getAuditedPark() {
      // console.log('查询已审核的车场');
      let token = wx.getStorageSync('token')
      if(!token) {return}
      let managerId = wx.getStorageSync("userId");
      const { data: res } = await auditedPark({ managerId });
      // console.log('查询已审核车场',res.data);
      const isSelectedPark = wx.getStorageSync('isSelectedPark')
      if (res.data.length > 0 ) {
        // 根据isSelectedPark判断是否选择过车场，如果有则使用选择后的车场
        if(isSelectedPark == false) {
          wx.setStorageSync("parkColumns", res.data);
          wx.setStorageSync("parkName", res.data[0].parkName);
          wx.setStorageSync("parkId", res.data[0].parkId);
          this.setData({
            parkName: res.data[0].parkName,
            parkColumns: res.data,
          });
        } else {
          this.setData({
            parkName:wx.getStorageSync('parkName'),
            parkColumns: res.data,
          });
        }
      }else {
        wx.setStorageSync("parkColumns", res.data);
      }
    },
    // 选择车场
    bindChangePark(e) {
      const {parkName,parkId} = this.data.parkColumns[e.detail.value]
      this.setData({
        parkName
      });
      console.log(this.data.parkColumns[e.detail.value]);
      wx.setStorageSync('parkName', parkName)
      wx.setStorageSync('parkId', parkId)
      wx.setStorageSync('isSelectedPark', true)
      this.triggerEvent(
        "bindChangeParkName",
        this.data.parkColumns[e.detail.value]
      );
    },
  },
});
